package com.hyc.myvideo.ui.activity.my;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.my.coupon.CouponAdapter;
import com.hyc.myvideo.data.api.constants.Constants;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.Coupon;
import com.hyc.myvideo.ui.base.BaseFActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import timber.log.Timber;

/**
 * 我的优惠券
 * @author gyb
 * @version 1.0
 * @created 2017/07/19
 */
public class CouponActivity extends BaseFActivity implements AdapterView.OnItemClickListener {
    @BindView(R.id.common_head_list_id)
    PullToRefreshListView toRefreshListView;

    /**页数*/
    private int page = 1;
    /**是否刷新*/
    private boolean isRefresh = true;

    private CouponAdapter adapter = null;
    private List<Coupon> listData = null;

    @Override
    public int getContentViewId() {
        return R.layout.activity_common_listview_head;
    }

    @Override
    protected void initView() {
        setLeftBtnClick(true, getIntent().getStringExtra("title"));
        toRefreshListView.setOnItemClickListener(this);
        /**设置间距*/
        ListView listView= toRefreshListView.getRefreshableView();
        listView.setDivider(new ColorDrawable(res.getColor(R.color.color_0)));
        listView.setDividerHeight(15);
        listData = new ArrayList<>();
        adapter = new CouponAdapter(this,listData,mListener);
        toRefreshListView.setAdapter(adapter);
        toRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                requestData(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                requestData(false);
            }
        });

    }

    @Override
    protected void initData() {
        requestData(true);
    }

    private void requestData(boolean isRefresh){
        this.isRefresh = isRefresh;
        if(isRefresh){
            page = 1;
        }else{
            page++;
        }
        api.requestCouponData(page);
    }



    @Override
    protected void oNext(String r, String method) {
        BaseResult<List<Coupon>> result = JSONObject.parseObject(r, new TypeReference<BaseResult<List<Coupon>>>(){});
        if(result.isCode()){
            if(isRefresh){
                listData.clear();
            }
            listData.addAll(result.getResult());
            adapter.updateListView(listData);
        }
        clearedListView();
    }

    @Override
    protected void oError() {
        clearedListView();
    }

    /***
     * 关闭刷新
     */
    private void clearedListView(){
        if(toRefreshListView.isRefreshing()){
            toRefreshListView.onRefreshComplete();
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    /***
     * 响应按钮点击事件
     */
    private CouponAdapter.MyClickListener mListener = new CouponAdapter.MyClickListener(){
        @Override
        public void myOnClick(Coupon coupon, View v) {
            Timber.i("==================CouponActivity======================"+coupon);
            startActivityForResult(new Intent(appContext, CouponDonatedActivity.class).putExtra("title", "优惠券转赠").putExtra("coupon",coupon),Constants.RESULT_CODE);
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case Constants.RESULT_CODE:
                try {
                    requestData(true);
                } catch (Exception e) {
                    Timber.e(e,"优惠券成功转赠返回");
                }
                break;
        }
    }

}